package com.wcx;

import com.taosdata.jdbc.TSDBDriver;

import javax.swing.text.rtf.RTFEditorKit;
import java.sql.*;
import java.util.Properties;

/**
 * 项目: TDengineDemo
 * <p>
 * 功能描述:
 *
 * @author: WuChengXing
 * @create: 2021-03-04 12:49
 **/
public class TDengineTest {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        TDengineTest tDengineTest = new TDengineTest();
        String sql = "select * from tb";
        ResultSet resultSet = tDengineTest.getResultSet(sql);
        Timestamp ts = null;
        int temperature = 0;
        float humidity = 0;
        while (resultSet.next()) {
            ts = resultSet.getTimestamp(1);
            temperature = resultSet.getInt(2);
            humidity = resultSet.getFloat("humidity");
            System.out.printf("%s, %d, %s\n", ts, temperature, humidity);
        }
    }

    public Connection getConn() throws Exception {
        Class.forName("com.taosdata.jdbc.TSDBDriver");
        String jdbcUrl = "jdbc:TAOS://taos.com:6030/db?user=root&password=taosdata";
        Properties connProps = new Properties();
        connProps.setProperty(TSDBDriver.PROPERTY_KEY_USER, "root");
        connProps.setProperty(TSDBDriver.PROPERTY_KEY_PASSWORD, "taosdata");
        connProps.setProperty(TSDBDriver.PROPERTY_KEY_CONFIG_DIR, "/etc/taos");
        connProps.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8");
        connProps.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8");
        connProps.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8");
        Connection conn = DriverManager.getConnection(jdbcUrl, connProps);
        return conn;
    }

    /**
     * 功能描述: 获取Statement
     *
     * @param
     * @return java.sql.Statement
     * @author WuChengxing
     * @date 2021/3/4
     */
    public Statement getState() {
        try {
            return getConn().createStatement();
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    /**
     * 创建数据库
     *
     * @param dbName
     * @throws SQLException
     */
    public void createDB(String dbName) throws SQLException {
        // create database
        getState().executeUpdate("create database if not exists " + dbName);
    }

    /**
     * 功能描述: 使用数据库
     *
     * @param dbName
     * @return void
     * @author WuChengxing
     * @date 2021/3/4
     */
    public void useDB(String dbName) throws SQLException {
        getState().executeUpdate("use " + dbName);
    }

    /**
     * 功能描述: 创建表结构
     *
     * @param sql
     * @return void
     * @author WuChengxing
     * @date 2021/3/4
     */
    public void createTable(String sql) throws SQLException {
        getState().executeUpdate(sql);
    }

    /**
     * 功能描述: 插入数据
     *
     * @param sql
     * @return int
     * @author WuChengxing
     * @date 2021/3/4
     */
    public int insert(String sql) throws SQLException {
        // insert data
        int affectedRows = getState().executeUpdate(sql);
        System.out.println("insert " + affectedRows + " rows.");
        return affectedRows;
    }

    /**
     * 功能描述: 获取返回集
     *
     * @param sql
     * @return java.sql.ResultSet
     * @author WuChengxing
     * @date 2021/3/4
     */
    public ResultSet getResultSet(String sql) throws SQLException {
        ResultSet resultSet = getState().executeQuery(sql);
        return resultSet;
    }
}
